package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController     //@RequestBody  +  @Controller
public class AddressController {

    //依赖注入service层
    @Autowired
    private AddressService addressService;

    /**
     * 查询地址列表
     * Request URL: http://localhost:8081/address/list
     * Request Method: GET
     */
    @GetMapping("/address/list")
    public ResultInfo findList(){
        //1.调用service层
        List<Address> addressList = addressService.findList();
        //2.返回响应
        return ResultInfo.success(addressList);
    }

    /**
     * 新增收货地址
     * Request URL: http://localhost:8081/address
     * Request Method: POST
     */
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address){   //接收请求体中的参数
        //1.调用service层
        addressService.save(address);
        //2.返回响应
        return ResultInfo.success();
    }

    /**
     * 设置(修改)默认地址
     * Request URL: http://localhost:8081/address/default
     * Request Method: PUT
     */
    @PutMapping("/address/default")
    public ResultInfo updateById(@RequestBody Map<String,Long> param){  //接收在请求体里的参数
        //1.获取参数
        Long id = param.get("id");
        //2.调用service层
        addressService.updateById(id);
        //3.返回响应
        return ResultInfo.success();
    }

    /**
     * (去结算)获取默认收货地址
     * Request URL: http://localhost:8081/address/default
     * Request Method: GET
     */
    @GetMapping("/address/default")
    public ResultInfo findDefaultAddress(){
        //1. 调用service层
        Address address = addressService.findDefaultAddress();
        //2. 返回响应
        return ResultInfo.success(address);
    }

    /**
     * 回显收货地址
     * Request URL: http://localhost:8081/address/1540612996341043200
     * Request Method: GET
     */
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable Long id){  //接收RESTful风格的参数
        //1.调用service层
        Address address = addressService.findById(id);
        //2.返回响应
        return ResultInfo.success(address);
    }

    /**
     * 修改收货地址
     * Request URL: http://localhost:8081/address
     * Request Method: PUT
     */
    @PutMapping("/address")
    public ResultInfo updateByAddress(@RequestBody Address address){    //接收请求体中的参数
        //1.调用service层
        addressService.updateByAddress(address);
        //2.返回响应
        return ResultInfo.success();
    }

    /**
     * 删除收货地址
     * Request URL: http://localhost:8081/address?ids=1540987085853429760
     * Request Method: DELETE
     * 考虑可扩展性的批量删除,传集合
     */
    @DeleteMapping("/address")
    public ResultInfo deleteById(@RequestParam List<Long> ids){     //接收普通参数
        //1.调用service层
        addressService.deleteById(ids);
        //2.返回响应
        return ResultInfo.success();
    }
}
